<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Http\Admin\Company\Controllers;

use App\Eloquent\Ygt\Logistics;
use App\Eloquent\Ygt\LogisticsNotes;
use App\Engine\Func;
use App\Http\Admin\Order\Models\Order;
use Framework\BaseClass\Http\Admin\Controller;

class LogisticsController extends Controller
{
    public function index(){}
    public function lists()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = ['company_id'=>$companyId];
            $lists          = Logistics::getList($where,'',$pageSize,$offset)->toArray();
            $count          = Logistics::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $result         = json_encode($data);
            return $result;
        } else {
            return $this->view('lists');
        }
    }
    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $data               = request()->all();
        $isSubmit           = isset($data['is_submit']) ? $data['is_submit'] : 0;
        $id                 = request('id',0);
        if($isSubmit){
            unset($data['is_submit']);
            $data['company_id']     = $companyId;
            $result                   = Logistics::edit($data);
            if( $result ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            $info                   = $data;
            if($id){
                $where              = ['id'=>$id];
                $info               = Logistics::getInfo($where)->toArray();
            }
            return $this->view('edit',compact('info'));
        }
    }
    public function delete(){}
    public function notesList()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $logisticsId        = request('logistics_id',0);
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = ['company_id'=>$companyId,'logistics_id'=>$logisticsId];
            $lists          = LogisticsNotes::getList($where,'',$pageSize,$offset)->toArray();
            $count          = LogisticsNotes::getCount($where);
            $isRemindFinishArr  = [0=>'不提醒',1=>'提醒'];
            foreach($lists as $key=>$val)
            {
                $valFinishTime      = $val['finish_time'];
                $valIsRemindFinish  = $val['is_remind_finish'];
                $finishTimeYmd      = '';
                if($valFinishTime > 0)
                {
                    $finishTimeYmd  = date('Y-m-d',$val['finish_time']);
                }
                $lists[$key]['finish_time_ymd'] = $finishTimeYmd;
                $lists[$key]['is_remind_finish'] = $isRemindFinishArr[$valIsRemindFinish];
            }
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $result         = json_encode($data);
            return $result;
        } else {
            $where          = ['id'=>$logisticsId];
            $logistics      = Logistics::getInfo($where);
            return $this->view('notes-list',compact('logistics'));
        }
    }
    public function notesEdit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $data               = request()->all();
        $id                 = isset($data['id']) ? $data['id'] : 0;
        $isSubmit           = isset($data['is_submit']) ? $data['is_submit'] : 0;
        $orderId            = isset($data['order_id']) ? $data['order_id'] : 0;
        if(!$orderId && !$id)
        {
            return $this->ajaxFail( '参数不全' );
        }
        if($isSubmit){
            unset($data['is_submit']);
            $data['company_id']     = $companyId;
            $result                   = LogisticsNotes::edit($data);
            if( $result ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            $info               = LogisticsNotes::getObject();
            if($id>0)
            {
                $where          = ['id'=>$id];
                $info           = LogisticsNotes::getInfo($where);
                $info->driving_license_img  = Func::getImgUrlById($info->driving_license);
                $info->driver_license_img   = Func::getImgUrlById($info->driver_license);
                if($info->finish_time>0 )
                {
                    $info->finish_time      = date('Y-m-d H:i:s',$info->finish_time);
                }
                $orderId        = $info->order_id;
            }
            $where              = ['company_id'=>$companyId];
            $logisticsList      = Logistics::getList($where);
            return $this->view('notes',compact('orderId','info','logisticsList'));
        }
    }
}